//合并区间
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) 
    {
        vector<vector<int>>res;
        if (intervals.size() == 0) {
            return res;
        }
        sort(intervals.begin(), intervals.end());
        res.push_back(intervals[0]);
        for(int i=1;i<intervals.size();i++)
        {
            //记得跟队尾元素比 而不是跟 intervals[i-1][1]比
            if(intervals[i][0]>res.back()[1])
            {
                res.push_back(intervals[i]);
                
            }
            else
            {
                res.back()[1]=max(intervals[i][1],res.back()[1]);
            }
        }
        return res;
    }
};
//自定义排序 数组相对排序
class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        unordered_map<int, int> rank;
        for (int i = 0; i < arr2.size(); ++i) {

            rank[arr2[i]] = i;
        }
        sort(arr1.begin(), arr1.end(), [&](int x, int y) {
            if (rank.count(x) && rank.count(y)) 
            {
                return rank[x] < rank[y];
            }
            else if(rank.count(x))
            {
                return true;
            }
            else if(rank.count(y))
            {
                return false;
            }
            else 
            {
                return x < y;
            }
        });
        return arr1;
    }
};
